package com.xwyzsn.food.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.xwyzsn.food.entity.Collection;
import com.xwyzsn.food.mapper.CollectionMapper;
import com.xwyzsn.food.service.CollectionService;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import net.sf.jsqlparser.util.validation.validator.UpdateValidator;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Map;

/**
 * <p>
 *  服务实现类
 * </p>
 *
 * @author scorpios
 * @since 2024-03-13
 */
@Service
public class CollectionServiceImpl extends ServiceImpl<CollectionMapper, Collection> implements CollectionService {

    @Autowired
    private CollectionMapper mapper;
    @Override
    public Object deleteOne(Map<String, ?> map) {
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        Integer id = Integer.parseInt(String.valueOf(map.get("id")));
        wrapper.eq("id",id);
        return mapper.delete(wrapper);
    }

    @Override
    public Object addOne(Map<String, ?> payload) {
        Integer userId = Integer.parseInt(String.valueOf(payload.get("userId")));
        Integer restId = Integer.parseInt(String.valueOf(payload.get("restId")));
        Collection entity = new Collection();
        entity.setUserId(userId);
        entity.setRestId(restId);
        return mapper.insert(entity);
    }

    @Override
    public Object get(Map<String, ?> payload) {
        if (payload.get("userId") == null){
            return mapper.selectList(null);
        }
        Integer userId = Integer.parseInt(String.valueOf(payload.get("userId")));
        QueryWrapper<Collection> wrapper = new QueryWrapper<>();
        wrapper.eq("user_id",userId);
        return mapper.selectList(wrapper);
    }
}
